/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.pac3.persistence.dao.impl;

import edu.uoc.tdp.pac3.configuration.PropertiesConfiguration;
import edu.uoc.tdp.pac3.persistence.dao.IAreaDao;
import edu.uoc.tdp.pac3.domain.Area;
import edu.uoc.tdp.pac3.persistence.exceptions.ErrorConnexioBD;
import edu.uoc.tdp.pac3.persistence.exceptions.ErrorSQL;
import edu.uoc.tdp.pac3.persistence.exceptions.ItemNoTrobat;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Gerard
 */
public class AreaDao extends BaseDao<Area> implements IAreaDao {

    public AreaDao() {
        super(PropertiesConfiguration.getInstance(), "tb_area");
    }

    @Override
    public List<Area> findAll() {
        List<Area> list = null;
        try {
            openConnection();
            ResultSet rs = buscar(getTableName());
            list = new ArrayList<Area>();

            while (rs.next()) {
                Area area = new Area();
                area.setId(rs.getInt("area_id"));
                area.setName(rs.getString("area_name"));
                area.setCreationDate(rs.getDate("creation_date"));
                list.add(area);
            }
        } catch (SQLException ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ItemNoTrobat ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ErrorSQL ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ErrorConnexioBD ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            tancarConnexio();
        }
        return list;

    }

    @Override
    public boolean insert(Area entity) {
        boolean result = false;
        try {
            openConnection();
            begin();
            StringBuilder sb = new StringBuilder();
            sb.append("INSERT INTO ").append(getTableName()).append("VALUES(?,?,?)");
            PreparedStatement statement = connexio.prepareStatement(sb.toString());
            statement.setInt(1, entity.getId());
            statement.setString(2, entity.getName());
            statement.setDate(3, new Date(entity.getCreationDate().getTime()));
            int i = statement.executeUpdate();
            if (i > 0) {
                result = true;
            }
            commit();
        } catch (ErrorSQL ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ErrorConnexioBD ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            tancarConnexio();
        }
        return result;
    }

    @Override
    public boolean update(Area entity) {
        boolean result = false;
        try {
            openConnection();
            begin();
            StringBuilder sb = new StringBuilder();
            sb.append("UPDATE ").append(getTableName()).append("SET ").append("area_name=?, ").append("creation_date=? ").append("WHERE area_id=?");
            PreparedStatement statement = connexio.prepareStatement(sb.toString());
            statement.setString(1, entity.getName());
            statement.setDate(2, new Date(entity.getCreationDate().getTime()));
            statement.setInt(3, entity.getId());
            int i = statement.executeUpdate();
            if (i > 0) {
                result = true;
            }
            commit();
        } catch (ErrorSQL ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ErrorConnexioBD ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            tancarConnexio();
        }
        return result;
    }

    @Override
    public boolean delete(Area entity) {
        boolean result = false;
        try {
            openConnection();
            begin();
            StringBuilder sb = new StringBuilder();
            sb.append("DELETE FROM ").append(getTableName()).append(" WHERE area_id=?");
            PreparedStatement statement = connexio.prepareStatement(sb.toString());
            statement.setInt(1, entity.getId());
            int i = statement.executeUpdate();
            if (i > 0) {
                result = true;
            }
            commit();
        } catch (ErrorSQL ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ErrorConnexioBD ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            tancarConnexio();
        }
        return result;
    }

    @Override
    public Area findById(Integer id) {
        Area area = null;
        try {
            openConnection();
            StringBuilder sb = new StringBuilder();
            sb.append("SELECT * FROM ").append(getTableName()).append(" WHERE area_id=?");
            PreparedStatement statement = connexio.prepareStatement(sb.toString());
            statement.setInt(1, id);
            ResultSet rs = statement.executeQuery();
            if (rs.next()) {
                area = new Area();
                area.setCreationDate(rs.getDate("creation_date"));
                area.setId(rs.getInt("area_id"));
                area.setName(rs.getString("area_name"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ErrorConnexioBD ex) {
            Logger.getLogger(AreaDao.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            tancarConnexio();
        }
        return area;
    }
}
